package clinicapoo.view.dialog;

import clinicapoo.exceptions.ValidationException;
import java.awt.Component;
import java.awt.TrayIcon.MessageType;
import javax.swing.JOptionPane;

import static clinicapoo.message.MessageResources.msg;

/**
 *
 * @author Fabio Montezuma
 */
public class Dialogs
{
    public static int NO = JOptionPane.NO_OPTION;
    public static int YES = JOptionPane.YES_OPTION;

    public static int showQuestion(Component c, String question)
    {
        return JOptionPane.showConfirmDialog(c, question,
                msg("view.dialog.confirmacao"), JOptionPane.YES_NO_OPTION);
    }

    public static void showError(Component c, Exception e)
    {
        if (e instanceof ValidationException)
        {
            ValidationException ve = (ValidationException) e;
            showError(c, ve);
        }
        else
        {
            JOptionPane.showMessageDialog(c,
                    e.getClass().getSimpleName() + ":\n\n" + e.getMessage(),
                    msg("view.dialog.erro"), MessageType.ERROR.ordinal());
        }
    }

    public static void showError(Component c, ValidationException e)
    {
        String erros = "";
        
        for(String erro : e.getErrorList())
        {
            erros += "- " + erro + "\n";
        }

        erros = erros.substring(0, erros.length()-1);

        JOptionPane.showMessageDialog(c, erros,
                msg("view.dialog.erro"), MessageType.ERROR.ordinal());
    }

    public static void showInfo(Component c, String message)
    {
        JOptionPane.showMessageDialog(c, message,
                msg("view.dialog.info"), MessageType.WARNING.ordinal());
    }
}
